﻿// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

using Hardware.Info;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Controllers;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Utilities.Encoders;

namespace Admin.NET.Core.Service;

/// <summary>
/// 系统通用服务 🧩
/// </summary>
[ApiDescriptionSettings(Order = 101)]
[AllowAnonymous]
public class SysCommonService : IDynamicApiController, ITransient
{
    private readonly IApiDescriptionGroupCollectionProvider _apiProvider;

    public SysCommonService(IApiDescriptionGroupCollectionProvider apiProvider)
    {
        _apiProvider = apiProvider;
    }

    /// <summary>
    /// 获取国密公钥私钥对 🏆
    /// </summary>
    /// <returns></returns>
    [DisplayName("获取国密公钥私钥对")]
    public SmKeyPairOutput GetSmKeyPair()
    {
        var kp = GM.GenerateKeyPair();
        var privateKey = Hex.ToHexString(((ECPrivateKeyParameters)kp.Private).D.ToByteArray()).ToUpper();
        var publicKey = Hex.ToHexString(((ECPublicKeyParameters)kp.Public).Q.GetEncoded()).ToUpper();

        return new SmKeyPairOutput
        {
            PrivateKey = privateKey,
            PublicKey = publicKey,
        };
    }

    /// <summary>
    /// 获取MD5加密字符串 🏆
    /// </summary>
    /// <param name="text"></param>
    /// <param name="uppercase"></param>
    /// <returns></returns>
    [DisplayName("获取MD5加密字符串")]
    public string GetMD5Encrypt(string text, bool uppercase = false)
    {
        return MD5Encryption.Encrypt(text, uppercase, is16: false);
    }

    /// <summary>
    /// 获取所有接口/动态API 🔖
    /// </summary>
    /// <param name="groupName"></param>
    /// <returns></returns>
    [DisplayName("获取所有接口/动态API")]
    public List<ApiOutput> GetApiList([FromQuery] string groupName = "")
    {
        var apiList = new List<ApiOutput>();

        //// 路由前缀
        //var defaultRoutePrefix = App.GetOptions<DynamicApiControllerSettingsOptions>().DefaultRoutePrefix;

        // 获取所有接口分组
        var apiDescriptionGroups = _apiProvider.ApiDescriptionGroups.Items;
        foreach (ApiDescriptionGroup group in apiDescriptionGroups)
        {
            if (!string.IsNullOrWhiteSpace(groupName) && group.GroupName != groupName)
                continue;

            var apiOuput = new ApiOutput
            {
                Name = string.IsNullOrWhiteSpace(group.GroupName) ? "系统接口" : group.GroupName,
                Route = "",
            };
            // 获取分组的所有接口
            var actions = group.Items;
            foreach (ApiDescription action in actions)
            {
                // 路由
                var route = action.RelativePath.Contains('{') ? action.RelativePath[..(action.RelativePath.IndexOf('{') - 1)] : action.RelativePath; // 去掉路由参数
                route = route[(route.IndexOf('/') + 1)..]; // 去掉路由前缀

                // 接口分组/控制器信息
                var controllerActionDescriptor = action.ActionDescriptor as ControllerActionDescriptor;
                if (controllerActionDescriptor == null)
                    continue;
                var apiDescription = controllerActionDescriptor.ControllerTypeInfo.GetCustomAttribute<ApiDescriptionSettingsAttribute>(true);
                var controllerName = controllerActionDescriptor.ControllerName;
                if (!apiOuput.Children.Exists(u => u.Name == controllerName))
                {
                    apiOuput.Children.Add(new ApiOutput
                    {
                        Name = controllerName,
                        Route = "",
                        Order = apiDescription?.Order ?? 0,
                    });
                }

                // 接口信息
                var apiController = apiOuput.Children.FirstOrDefault(u => u.Name.Equals(controllerName));
                var displayName = controllerActionDescriptor.MethodInfo.GetCustomAttribute<DisplayNameAttribute>(true)?.DisplayName;
                apiController.Children.Add(new ApiOutput
                {
                    Name = displayName,
                    Route = route,
                    HttpMethod = action.HttpMethod,
                    Order = apiDescription?.Order ?? 0,
                });

                // 接口分组/控制器排序
                apiOuput.Children = apiOuput.Children.OrderByDescending(u => u.Order).ToList();
            }

            apiList.Add(apiOuput);
        }
        return apiList;
    }

    /// <summary>
    /// 下载标记错误的临时 Excel（全局） 🔖
    /// </summary>
    /// <returns></returns>
    [DisplayName("下载标记错误的临时 Excel")]
    public async Task<IActionResult> DownloadErrorExcelTemp([FromQuery] string fileName = null)
    {
        var userId = App.User?.FindFirst(ClaimConst.UserId)?.Value;
        var resultStream = App.GetRequiredService<SysCacheService>().Get<MemoryStream>(CacheConst.KeyExcelTemp + userId);

        if (resultStream == null)
            throw Oops.Oh("错误标记文件已过期。");

        return await Task.FromResult(new FileStreamResult(resultStream, "application/octet-stream")
        {
            FileDownloadName = $"{(string.IsNullOrEmpty(fileName) ? "错误标记＿" + DateTime.Now.ToString("yyyyMMddhhmmss") : fileName)}.xlsx"
        });
    }

    /// <summary>
    /// 获取机器序列号 🔖
    /// </summary>
    /// <returns></returns>
    public string GetMachineSerialKey()
    {
        try
        {
            HardwareInfo hardwareInfo = new();
            hardwareInfo.RefreshBIOSList(); // 刷新 BIOS 信息
            hardwareInfo.RefreshMotherboardList(); // 刷新主板信息
            hardwareInfo.RefreshCPUList(false); // 刷新 CPU 信息

            var biosSerialNumber = hardwareInfo.BiosList.MinBy(u => u.SerialNumber)?.SerialNumber;
            var mbSerialNumber = hardwareInfo.MotherboardList.MinBy(u => u.SerialNumber)?.SerialNumber;
            var cpuProcessorId = hardwareInfo.CpuList.MinBy(u => u.ProcessorId)?.ProcessorId;
            // 根据 BIOS、主板和 CPU 信息生成 MD5 摘要
            var md5Data = MD5Encryption.Encrypt($"{biosSerialNumber}_{mbSerialNumber}_{cpuProcessorId}", true);
            var serialKey = $"{md5Data[..8]}-{md5Data[8..16]}-{md5Data[16..24]}-{md5Data[24..]}";
            return serialKey;
        }
        catch (Exception ex)
        {
            throw Oops.Oh($"获取机器码失败：{ex.Message}");
        }
    }
}